package com.dts.yjnb.service.service.impl;

import com.dts.common.core.page.TableDataInfo;
import com.dts.common.exception.ServiceException;
import com.dts.yjnb.service.domain.Market;
import com.dts.yjnb.service.domainDto.MarketDTO;
import com.dts.yjnb.service.mapper.MarketMapper;
import com.dts.yjnb.service.service.IMarketService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 农贸市场 服务层实现
 *
 * @author dts
 */
@Service
public class MarketServiceImpl implements IMarketService
{
    @Autowired
    private MarketMapper marketMapper;


    @Override
    public TableDataInfo pageQuery(MarketDTO marketDTO) {
        //属性拷贝（包括beginTime和endTime）
        Market market = new Market();
        BeanUtils.copyProperties(marketDTO, market);

        // 开始分页
        PageHelper.startPage(marketDTO.getPage(), marketDTO.getPageSize());
        Page<Market> page = marketMapper.pageQuery(market);

        long total = page.getTotal();
        List<Market> records = page.getResult();
        // 封装返回结果
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(total);
        tableDataInfo.setRows(records);
        return tableDataInfo;
    }

    @Override
    public Market getByid(Long id) {
        // 参数校验
        if (id == null) {
            throw new ServiceException("农贸市场ID不能为空");
        }

        // 查询市场详情
        Market market = marketMapper.getByid(id);

        // 校验查询结果
        if (market == null) {
            throw new ServiceException("农贸市场不存在");
        }
        return market;
    }

    /**
     * 新增农贸市场
     *
     * @param marketDTO 农贸市场信息
     */
    @Override
    public void add(MarketDTO marketDTO) {
        // 参数校验
        if (marketDTO == null) {
            throw new ServiceException("农贸市场信息不能为空");
        }
        if (marketDTO.getMarketName() == null || marketDTO.getMarketName().trim().isEmpty()) {
            throw new ServiceException("市场名称不能为空");
        }
        if (marketDTO.getRegion() == null || marketDTO.getRegion().trim().isEmpty()) {
            throw new ServiceException("所在地区不能为空");
        }

        // DTO转换为实体类
        Market market = new Market();
        BeanUtils.copyProperties(marketDTO, market);

        // 执行插入
        int count = marketMapper.add(market);

        // 校验插入结果
        if (count == 0) {
            throw new ServiceException("新增失败");
        }
    }

    /**
     * 市场批量删除
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new ServiceException("请选择要删除的市场");
        } else {

            Integer count = marketMapper.deleteByids(ids);
            if (count == 0) {
                throw new ServiceException("删除失败，市场不存在");
            }
        }
    }

    /**
     * 农贸市场编辑
     *
     * @param marketDTO 农贸市场信息
     */
    @Override
    public void update(MarketDTO marketDTO) {
        // 参数校验
        if (marketDTO == null || marketDTO.getMarketId() == null) {
            throw new ServiceException("农贸市场ID不能为空");
        }

        // DTO转换为实体类
        Market market = new Market();
        BeanUtils.copyProperties(marketDTO, market);

        // 执行更新
        int count = marketMapper.updateByid(market);

        // 校验更新结果
        if (count == 0) {
            throw new ServiceException("修改失败，农贸市场不存在");
        }
    }
}

